package com.ominext.home;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.ominext.beautifulcapture.R;
import com.ominext.camera.CustomCameraActivity;
import com.ominext.facebook.HomeFacebook;
import com.ominext.twitter.TwitterHome;
import com.ominext.utils.DeviceUtils;
import com.ominext.utils.ImageUtils;

public class HomeActivity extends Activity implements OnClickListener {
	ImageButton mButtonGallery, mButtonFacebook, mButtonTweeter, mButtonCamera;
	ImageView mImageView;
	private static int RESULT_LOAD_IMAGE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mButtonGallery = (ImageButton) findViewById(R.id.imageButtonGallery);
		mButtonGallery.setOnClickListener(this);
		mImageView = (ImageView) findViewById(R.id.imageHome);
		if (Path.imageShowPath != null) {
			Bitmap bitmap = BitmapFactory.decodeFile(Path.imageShowPath);

			mImageView.setImageBitmap(ImageUtils.rotate(Path.imageShowPath));
			Toast.makeText(this, Path.imageShowPath, Toast.LENGTH_LONG).show();
		}
		mImageView.setOnClickListener(this);
		mButtonFacebook = (ImageButton) findViewById(R.id.imageFacebook);
		mButtonFacebook.setOnClickListener(this);
		mButtonTweeter = (ImageButton) findViewById(R.id.imageTweeter);
		mButtonTweeter.setOnClickListener(this);
		mButtonCamera = (ImageButton) findViewById(R.id.imageButtonCapture);
		mButtonCamera.setOnClickListener(this);
	}

	// set onClick method
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		// go to image gallery
		case R.id.imageButtonGallery:
			Intent mIntent = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(mIntent, RESULT_LOAD_IMAGE);
			break;
		// show image selected
		case R.id.imageHome:
			Intent intent = new Intent(HomeActivity.this, ShowImage.class);
			startActivity(intent);
			break;
		case R.id.imageFacebook:
			Intent facebook_intent = new Intent(HomeActivity.this,
					HomeFacebook.class);
			startActivity(facebook_intent);
			break;
		case R.id.imageButtonCapture:
			Intent camera_intent = new Intent(HomeActivity.this,
					CustomCameraActivity.class);
			startActivity(camera_intent);
			break;
		case R.id.imageTweeter:
			Intent tw_intent = new Intent(HomeActivity.this, TwitterHome.class);
			startActivity(tw_intent);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
				&& data != null) {
			// get uri image selected
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };
			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			// get path of image selected
			String picturePath = cursor.getString(columnIndex);
			// save path of image by property of class path
			Path.imageShowPath = picturePath;
			cursor.close();
			// convert image to bitmap
			Bitmap image = BitmapFactory.decodeFile(picturePath);
			// resize image
			image = ImageUtils.resizeBitmap(image, mImageView.getHeight(),
					mImageView.getWidth());
			
			// set image bitmap for ImageView in home
			mImageView.setImageBitmap(image);
			Toast.makeText(this, Path.imageShowPath, Toast.LENGTH_LONG).show();
			// mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
